﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Linq;
using System.Web;

namespace Gollie.DataTables
{
	public class OrderBasketDataItem
	{
		public uint OfferId { get; set; }
		public string Bedrijfsnaam { get; set; }
		public decimal Credit { get; set; }
		public decimal Price { get; set; }
		public int Number { get; set; }
		public decimal TotalCredit { get; set; }
		public decimal TotalPrice { get; set; }
		public uint VendorId { get; set; }
		public string Description { get; set; }

		public bool HasDescription()
		{
			return string.IsNullOrWhiteSpace(this.Description) == false;
		}
	}

	public class OrderBasketData : BaseBindable<OrderBasketDataItem>
	{
		public OrderBasketData()
		{
		}

		public void Add(int offerId, string bedrijfsnaam, decimal credit, decimal price, int number, uint vendorId, string description)
		{
			OrderBasketDataItem item = this.GetOffer(offerId);
			if (item == null && number == 0)
				return;

			if (item != null && number == 0)
			{
				this.Remove(item);
				return;
			}

			if (item == null)
			{
				item = new OrderBasketDataItem();
				this.Add(item);
			}

			item.Bedrijfsnaam = bedrijfsnaam;
			item.Credit = credit;
			item.Description = description;
			item.Number = number;
			item.OfferId = (uint)offerId;
			item.Price = price;
			item.VendorId = vendorId;
			item.TotalCredit = credit * number;
			item.TotalPrice = price * number;
		}

		public void DeleteOffer(int offerId)
		{
			OrderBasketDataItem item = this.GetOffer(offerId);
			if (item != null)
				this.Remove(item);
		}

		public void Sum(out decimal credit, out decimal price)
		{
			//credit = 0;
			//price = 0;

			credit = this.Sum(i => i.TotalCredit);
			price = this.Sum(i => i.TotalPrice);

			//foreach (DataRow row in this.Rows)
			//{
			//	credit += (decimal)row[5];
			//	price += (decimal)row[6];
			//}
		}

		public OrderBasketDataItem GetOffer(int offerId)
		{
			return this.SingleOrDefault(x => x.OfferId == (uint)offerId);
		}
	}
}